﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AStar.LogicClass;
using AStar.Collections;
using AStar.Class;

namespace AStar
{
    public partial class Display : UserControl
    {
        private Logic log;
        private float height = 5;
        private float width = 5;
        private MyList<Tuple<City,Rectangle>> cityrecs;
        private MyList<City> shortestRoute = null;
        private City from = null;
        private City to = null;



        internal Logic Log
        {
            get { return log; }
            set { log = value; }
        }
        public Display()
        {
            InitializeComponent();
            log = new Logic();
            cityrecs = new MyList<Tuple<City,Rectangle>>();
        }

        private void Display_Load(object sender, EventArgs e)
        {

        }

        public void ShortestRoute(City from, City to)
        {
            this.from = from;
            this.to = to;
            shortestRoute = log.GetShortestRoute(from, to);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            for (int i = 0; i < log.Cities.Count(); i++)
            {
                Rectangle temp = new Rectangle(((int)(log.Cities.Get(i).X - width)), ((int)(log.Cities.Get(i).Y - height)), (int)width * 2, (int)height * 2);
                bool exists = false;
                for (int j = 0; j < cityrecs.Count(); j++)
                {
                    if (cityrecs.Get(j).Item2.Equals(temp))
                    {
                        exists = true;
                        break;
                    }
                }
                if (!exists)
                {
                    cityrecs.Add(Tuple.Create(log.Cities.Get(i),new Rectangle(temp.X, temp.Y, temp.Width, temp.Height)));
                }
                //e.Graphics.DrawEllipse(Pens.AliceBlue, temp);
               
            }
            
            //MyList<String> done = new MyList<string>();
            paintStreets(e.Graphics);

            paintShortestRoute(e.Graphics);
            
            paintCities(e.Graphics);
            
        }

        private void paintStreets(Graphics graphics)
        {
            for (int city2bdone = 0; city2bdone < log.Cities.Count(); city2bdone++)
            {
                String now = log.Cities.Get(city2bdone).Name;
                for (int dest = 0; dest < log.Streets[now].Count(); dest++)
                {
                    /*bool exists = false;
                    for (int i = 0; i < done.Count(); i++)
                    {
                        if (log.Streets[now].Get(dest).Name.Equals(done.Get(i)))
                        {
                            exists = true;
                            break;
                        }
                    }
                    if (exists)
                    {
                        String doneCity = log.Streets[now].Get(dest).Name;
                        for (int j = 0; j < log.Streets[doneCity].Count(); j++)
                        {
                            exists = false;
                            if (log.Streets[doneCity].Get(j).Name.Equals(now))
                            {
                                exists = true;
                                break;
                            }
                            if (!exists)
                            {
                                e.Graphics.DrawLine(Pens.Black, new PointF(log.Cities.Get(city2bdone).X, log.Cities.Get(city2bdone).Y), new PointF(log.Streets[now].Get(dest).X, log.Streets[now].Get(dest).Y));
                            }
                        }
                    }
                    else
                    {
                        e.Graphics.DrawLine(Pens.Black, new PointF(log.Cities.Get(city2bdone).X, log.Cities.Get(city2bdone).Y), new PointF(log.Streets[now].Get(dest).X, log.Streets[now].Get(dest).Y));
                    }*/
                    graphics.DrawLine(Pens.Black, new PointF(log.Cities.Get(city2bdone).X, log.Cities.Get(city2bdone).Y), new PointF(log.Streets[now].Get(dest).X, log.Streets[now].Get(dest).Y));
                }
                //done.Add(now);
            }
        }

        private void paintShortestRoute(Graphics graphics)
        {
            if (shortestRoute != null)
            {
                for (int i = 0; i < shortestRoute.Count() - 1; i++)
                {
                    graphics.DrawLine(Pens.Red, shortestRoute.Get(i).X, shortestRoute.Get(i).Y, shortestRoute.Get(i + 1).X, shortestRoute.Get(i + 1).Y);
                }
            }
        }

        private void paintCities(Graphics graphics)
        {
            for (int j = 0; j < cityrecs.Count(); j++)
            {
                if (cityrecs.Get(j).Item2.Contains(new Point((int)from.X, (int)from.Y)) || cityrecs.Get(j).Item2.Contains(new Point((int)to.X, (int)to.Y)))
                {
                    graphics.FillEllipse(Brushes.Red, cityrecs.Get(j).Item2);
                }
                else
                {
                    graphics.FillEllipse(Brushes.Black, cityrecs.Get(j).Item2);
                }
            }
        }
    }
}
